package com.nicolasprof.profgallery.effects {
	import gs.TweenLite;

	import com.gskinner.utils.Rndm;
	import com.nicolasprof.profgallery.Engine;
	import com.nicolasprof.profgallery.effects.Effect;

	import flash.display.Bitmap;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.geom.Point;

	/**
	 * @author Nicolas Prof
	 */
	public class ZoomEffect extends Effect {

		public function ZoomEffect() {
			super();
		}
		override public function build(...$layers) : void {
			var bitmap : Bitmap = $layers[0];
			var width : Number = bitmap.width;
			var height : Number = bitmap.height;
			var mask : Sprite = $layers[1];
			var config: XML = getConfig();
			var zoomLayer : Sprite = new Sprite();
			
			var point : Point = getPos(width, height, config);
			
			
			var parent : DisplayObjectContainer = bitmap.parent;
			var index : int = parent.getChildIndex(bitmap);
			zoomLayer.addChild(parent.removeChild(bitmap));
			parent.addChildAt(zoomLayer, index);
			zoomLayer.mask = mask;
			
			mask.graphics.beginFill(0xFF0000);
			mask.graphics.drawRect(0, 0, width, height);
			mask.graphics.endFill();
			
			bitmap.x = -point.x;
			bitmap.y = -point.y;
			zoomLayer.x = point.x;
			zoomLayer.y = point.y;
			
			zoomLayer.scaleX = zoomLayer.scaleY = 4;		
			
			TweenLite.to(zoomLayer, config.@time, {scaleX:1, scaleY:1});
		}
		

		override protected function getConfig() : XML {
			var config : XML = Engine.CONFIG_XML;
			return config.effects.zoom[0];
		}
		private function getPos($width : Number, $height : Number, $config : XML) : Point {
			var values : Array = String($config.@point).split(",");
			var pnt : Point = new Point();
			if(values[0]== "random") {
				Rndm.seed = Math.random() * 0xFFFFFF;
				pnt.x = Rndm.integer($width);
				pnt.y = Rndm.integer($height);
			} else {
				values.length = 2;
				pnt.x = int(values[0]);
				pnt.y = int(values[1]);
			}
			return pnt;
		}
		
	}
}
